package com.hos.zxing.slice;

import com.hos.zxing.ResourceTable;
import com.mylhyl.zxing.scanner.encode.QREncode;
import com.mylhyl.zxing.scanner.scan.ability.ScanAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Image mImage;
    private Button mSanBtn, mCreateBtn;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        mImage = (Image) findComponentById(ResourceTable.Id_image);
        mSanBtn = (Button) findComponentById(ResourceTable.Id_scanBtn);
        mCreateBtn = (Button) findComponentById(ResourceTable.Id_createBtn);
        mSanBtn.setClickedListener(this::onClick);
        mCreateBtn.setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_scanBtn:
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(ScanAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                break;
            case ResourceTable.Id_createBtn:
                new QREncode(this)
                        .setQrBackground(ResourceTable.Color_white)
                        .build(mImage, "This is a qrCode");
                break;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
